/* The code is based on the following paper:                                                                                                            */
/*   Chen, T. Y., Lin, Y. L., and Tzeng, L. Y., forthcoming.                                                                                    */
/*   Estimating probability weighting functions through option pricing bounds. Review of Asset Pricing Studies. */
/*                                                                                                                                                                               */
/* Copyright: Tzu-Ying Chen, Yo-Lan Lin, Larry Y. Tzeng                                                                                    */
/* Date: January 30, 2024                                                                                                                                         */

libname OMO 'E:\WRDS Data\���Y��\';
libname OMS 'E:\WRDS Data\���Y��\';

%MACRO OP (FileName1, FileName2, i, j, p, q); 
PROC PRINTTO log = "E:\WRDS Data\Record.log";
RUN;

data op;
set OMO.&FileName1;
where exercise_style = 'E' and index_flag = '1' and ss_flag = '0' and am_settlement = 1;
run;

data op;
set op;
keep secid cusip ticker date exdate cp_flag strike_price best_bid best_offer open_interest volume impl_volatility delta gamma theta vega issue_type forward_price symbol;
run;

data op;
set op;
if date = . then delete;
if exdate = . then delete;
if strike_price = . then delete;
if best_bid = . then delete;
if best_offer = . then delete;
if open_interest = . then delete;
if volume = . then delete;
run;

data op;
set op;
symbol = SUBSTR(symbol, 1, 4);
run;

data op;
set op;
if symbol = 'SPXW' then delete; 
run;

data op;
set op; 
if missing(cusip) then cusip = '?';
if missing(ticker) then ticker = '?';
if missing(issue_type) then issue_type = '?';
run;

data op;
set op;
strike_price = strike_price / 1000;
TtoMat = exdate - date;
date_year = year(date);
date_month = month(date);
run;

data op;
set op;
if cp_flag = 'C' then do;
cp_flag = 1;
end;
else do;
cp_flag = 2;
end;
run;

data op;
set op;
keep secid cusip ticker date date_year date_month TtoMat cp_flag strike_price best_bid best_offer open_interest volume impl_volatility delta gamma theta vega issue_type forward_price;
run;

data sp;
set OMS.&FileName2;
keep secid date close; 
run;

data sp;
set sp;
if date = . then delete; 
if close = . then delete; 
run;

data sp;
set sp;
close = abs(close);
run;

PROC SORT DATA = op OUT= op;
BY secid date;
RUN;

PROC SORT DATA = sp OUT= sp;
BY secid date;
RUN;

data opsp; 
merge op(in = a) sp(in = b);
by secid date;
if a = 1 and b = 1;
run;

data opsp;
set opsp;
keep secid cusip ticker date date_year date_month TtoMat cp_flag strike_price close best_bid best_offer open_interest volume impl_volatility delta gamma theta vega issue_type forward_price;
run;

PROC SORT DATA = opsp OUT = opsp;
BY secid date TtoMat cp_flag strike_price;
RUN; 

%do y = &i %to &j;
		%do m = &p %to &q;
				data opsp_y_m;
				set opsp;
				where date_year = &y and date_month = &m;
				run;

       		    data _null_;                                         
      		    set opsp_y_m;                                        
      		    file "E:\WRDS Data\OP&y._&m..txt";
     		    put secid date TtoMat cp_flag strike_price close best_bid best_offer open_interest volume;
     		    run;

    	        data _null_;                                         
    		    set opsp_y_m;                                        
    	        file "E:\WRDS Data\OP&y._&m._ID.txt";
   		        put secid date cusip ticker issue_type;
     		    run;
		%end;
%end;
%MEND OP;

%OP (IndexOptions19962021_SP500, SecurityPrices19962021, 1996, 2021, 1, 12);
